package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author : sunjun
 * @email : sunjun0207@163.com
 * @create 2019/11/10 15:55
 */
//预约设置
@RestController
@RequestMapping("/ordersetting")

public class OrderSettingController {
    //注入orderSettingService用来操作service
    @Reference
    private OrderSettingService orderSettingService;

    /*
    预约设置-->根据指定日期修改可预约人数
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        //程序健壮性和安全性处理
        try {
            //调用orderSettingService完成修改
            orderSettingService.editNumberByDate(orderSetting);
            //修改可预约人数成功
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }


    /*
    预约设置-->根据日期查询预约设置数据2019-06
     */
    @RequestMapping("/findOrderSettingByMonth")
    public Result findOrderSettingByMonth(String date){//date格式为：yyyy-MM
        //程序健壮性和安全性处理
        try {
            //调用orderSettingService完成查询
            List<Map> list = orderSettingService.findOrderSettingByMonth(date);
            //获取指定月中日期的预约设置数据差
            return new Result(true,MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


    /*
    excel文件上传,并解析文件内容到数据库
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
        //程序健壮性和安全性处理
        try {
            //调用POIUtils读取Excel文件数据:多个工作簿,多条数据
            List<String[]> list = POIUtils.readExcel(excelFile);
            if(list != null && list.size() > 0){//Excel文件存在数据
                //定义一个泛型为:预约设置对象orderSetting,arraylist存放预约设置列表展示
                List<OrderSetting> orderSettingList = new ArrayList<>();
                //遍历list集合
                for (String[] strings : list) {
                    //取出数据:预约时间,可预约人数
                    String orderDate = strings[0];
                    String number = strings[1];
                    //将数据封装到预约设置orderSetting对象中
                    OrderSetting orderSetting = new OrderSetting(new Date(orderDate), Integer.parseInt(number));
                    //将预约设置对象orderSetting存到集合
                    orderSettingList.add(orderSetting);
                }
                //调用orderSettingService完成添加
                orderSettingService.add(orderSettingList);
            }

        } catch (IOException e) {
            e.printStackTrace();
            //文件上传失败,返回提示信息
            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //文件上传成功,返回提示信息
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }

}
